import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";

@IonicPage({
    // segment: 'information/add/:kind/:type_id',
    defaultHistory:['HomePage']
})
@Component({
  selector: 'page-stores',  
  templateUrl: 'stores.html',
})
export class StoresPage {
  options: any;
    type_name: any;
    template: any;
    infoData = [];
    infoBuildData = [];
    infoIdData = [];
    auto_content = '';
    checkStatus = [];
    infoTextData = [];
    //需要发送的数据
   
    userInfo: any;
    amoy_id: number;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    ionViewCanEnter(){
        return this.validate.checkLogin();
        
    }
    ionViewWillEnter() {
        if (!this.validate.checkLogin()) {//登录
            this.navCtrl.push('LoginPage', {'backPage': 'MemberPage'});
        } else {
            let userInfo = this.storage.get('userInfo');
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo);
            }
        }
        this.getOptions();
    }

    //获取表单数据
    getOptions() {
        let loading = this.util.loading();
        loading.show();
        this.httpService.requestPostData('/task_info/get_option', {
            type_id: 1,
            kind: 'information'
        },data => {
            loading.close();
            if (data['code'] === 200) {
                this.options = data['data']['data'];
                this.type_name = data['data']['type_name'];
                this.template = data['data']['tpl'];
                this.storage.set('rate',JSON.stringify(data['data']['rate']));
            } else {
                alert(data['message']);
            }

        });
    }

    //选择多选数据
    selCheckData(item, option) {
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        if (this.util.inArray(this.infoData[item.letter], option.list_name) < 0) {
            this.infoData[item.letter].push(option.list_name);
            this.infoBuildData[item.num].push(option.list_name);
            this.infoIdData[item.num].push(option.list_id);
        } else {
            let index = this.util.inArray(this.infoData[item.letter], option.list_name);
            if (index !== -1) {
                this.infoData[item.letter].splice(index, 1);
                this.infoBuildData[item.num].splice(index, 1)
                this.infoIdData[item.num].splice(index, 1)
            }
        }
    }

    //单选数据
    selRadioData(item, option) {
        if(option.list_name == "不限"){
            this.infoBuildData[item.num]=['太原','迎泽区','小店区','杏花岭区','万柏林区','晋源区','尖草坪区','太原周边'];
        }else{
            this.infoBuildData[item.num] = option.list_name;
        }
        
        this.infoIdData[item.num] = option.list_id;
        this.infoTextData[item.letter] = '';
       
    }

    //文本框数据
    textData(item) {
        if(this.infoData[item.letter] == '' || this.infoData[item.letter] == undefined){
            return;
        }
        this.infoBuildData[item.num] = this.infoData[item.letter];
        this.infoIdData[item.num] = this.infoData[item.letter];
        
    }

    //多选文本框数据
    checkFocusData(item) {
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        let index = this.util.inArray(this.infoData[item.letter], this.infoTextData[item.letter]);
        if (index !== -1) {
            this.infoData[item.letter].splice(index, 1);
            this.infoBuildData[item.num].splice(index, 1);
            this.infoIdData[item.num].splice(index, 1);
        }
     
    }

    checkBlurData(item) {
        if (this.infoTextData[item.letter] == undefined || this.infoTextData[item.letter] == '') {
            return false;
        }
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        this.infoData[item.letter].push(this.infoTextData[item.letter]);
        this.infoBuildData[item.num].push(this.infoTextData[item.letter]);
        this.infoIdData[item.num].push(this.infoTextData[item.letter]);
        
    }



    resetData() {
        this.infoData = [];
        this.infoBuildData = [];
        this.infoTextData = [];
    }

    jumpPage(){
        this.navCtrl.push('InformationPage',{nav_id:37})
    }

    /**
     * 数据提交
     * */
    submitData() {
        this.storage.set('resumeSearchData',JSON.stringify(this.infoBuildData));
        this.navCtrl.push('InformationPage', {nav_id: 1, searchMethod:'advanced'});
        return;
    }
    
}
